﻿using PLCTest.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace PLCTest.BO
{
    public class PlanarGeneralWriteWcsAction
    {
        public PlanarGeneralWriteWcsAction() { }
        public PlanarGeneralWriteWcsAction(
             int equipmentCode
            , short actionType
            , int toEquipmentCode
            , int taskCode
            , string palletBarcode
            , byte palletType
            , byte alrmCode
            , byte reserve = 0
            , byte reserve2 = 0
            , byte reserve3 = 0
            , byte reserve4 = 0)
        {
            EquipmentCode = equipmentCode;
            ActionType = actionType;
            ToEquipmentCode = toEquipmentCode;
            TaskCode = taskCode;
            PalletBarcode = palletBarcode;
            PalletType = palletType;
            AlrmCode = alrmCode;
            Reserve = reserve;
            Reserve2 = reserve2;
            Reserve3 = reserve3;
            Reserve4 = reserve4;
        }

        /// <summary>
        /// 设备号
        /// </summary>
        public int EquipmentCode { get; set; }
        /// <summary>
        /// 任务号
        /// </summary>
        public int TaskCode { get; set; }
        /// <summary>
        /// 目标设备号
        /// </summary>
        public int ToEquipmentCode { get; set; }
        /// <summary>
        /// 托盘码
        /// </summary>
        public string PalletBarcode { get; set; }
        /// <summary>
        /// 握手类型
        /// </summary>
        public short ActionType { get; set; }
        /// <summary>
        /// 托盘类型
        /// </summary>
        public byte PalletType { get; set; }
        /// <summary>
        /// 报警代码
        /// </summary>
        public byte AlrmCode { get; set; }

        /// <summary>
        /// 单机任务标识
        /// </summary>
        public byte Reserve { get; set; }
        /// <summary>
        /// 预留2
        /// </summary>
        public byte Reserve2 { get; set; }
        /// <summary>
        /// 预留3
        /// </summary>
        public byte Reserve3 { get; set; }
        /// <summary>
        /// 预留4
        /// </summary>
        public byte Reserve4 { get; set; }

        public byte[] Deserialize()
        {

            byte[] bytes = new byte[40];
            HelperByte.SetDIntAt(bytes, 0, EquipmentCode);
            HelperByte.SetDIntAt(bytes, 4, TaskCode);
            HelperByte.SetDIntAt(bytes, 8, ToEquipmentCode);

            HelperByte.SetByteAt(bytes, 12, 18);
            byte length = Convert.ToByte(PalletBarcode.Length);
            HelperByte.SetByteAt(bytes, 13, length);
            HelperByte.SetCharsAt(bytes, 14, PalletBarcode);

            HelperByte.SetIntAt(bytes, 32, ActionType);
            HelperByte.SetByteAt(bytes, 34, PalletType);
            HelperByte.SetByteAt(bytes, 35, AlrmCode);
            HelperByte.SetByteAt(bytes, 36, Reserve);
            HelperByte.SetByteAt(bytes, 37, Reserve2);
            HelperByte.SetByteAt(bytes, 38, Reserve3);
            HelperByte.SetByteAt(bytes, 39, Reserve4);

            return bytes;
        }

    }
}
